package com.suning.utils;

import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.JestResult;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.core.Delete;
import io.searchbox.core.Index;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 〈一句话功能简述〉<br>
 * 〈功能详细描述〉
 *
 * @author 16080951(林健)
 * @Copyright (c) 2016 苏宁软件技术有限公司 版权所有
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
public class JestUtils {

    //IP地址。
    private final static String IPHost = "http://10.19.41.221:9200";

    private static JestClient jestClient;

    private static Logger logger = LoggerFactory.getLogger(JestUtils.class);


    /**
     * 获取客户端
     *
     * @return
     */
    private static JestClient clientConfig() {
        JestClientFactory factory = new JestClientFactory();
        factory.setHttpClientConfig(new HttpClientConfig
                .Builder(IPHost)
                .multiThreaded(true)
                .build());
        return factory.getObject();
    }

    /**
     * 获取一个jest的对象
     *
     * @return
     */
    public static JestClient getInstance() {
        if (jestClient == null) {
            jestClient = clientConfig();
        }
        return jestClient;
    }

    /**
     * 插入或更新文档
     *
     * @param indexId
     * @param indexObject
     * @param indexName
     * @param indexType
     * @return
     */
    public static boolean insertOrUpdateDoc(String indexId, Object indexObject, String indexName, String indexType) {
        Index.Builder builder = new Index.Builder(indexObject);
        //builder.setHeader(PWDKEY, getSecret());
        builder.id(indexId);
        builder.refresh(true);
        Index index = builder.index(indexName).type(indexType).build();
        try {
            JestResult result = getInstance().execute(index);
            if (result != null && !result.isSucceeded()) {
                throw new RuntimeException(result.getErrorMessage() + "插入更新文档失败!");
            }
        } catch (Exception e) {
            logger.error("插入更新文档失败", ExceptionUtils.getFullStackTrace(e));
            return false;
        }
        return true;
    }

    /**
     * 删除文档
     *
     * @param indexId
     * @param indexName
     * @param indexType
     */
    public static boolean deleteDoc(String indexId, String indexName, String indexType) {
        Delete.Builder builder = new Delete.Builder(indexId);
        builder.refresh(true);
        Delete delete = builder.index(indexName).type(indexType).build();
        try {
            JestResult result = getInstance().execute(delete);
            if (result != null && !result.isSucceeded()) {
                throw new RuntimeException(result.getErrorMessage() + "删除文档失败!");
            }
        } catch (Exception e) {
            logger.error("删除文档失败", ExceptionUtils.getFullStackTrace(e));
            return false;
        }
        return true;
    }

    /**
     * 删除类型
     * @param indexId
     * @param indexName
     * @param indexType
     */
    /*public boolean deleteType( String indexName, String indexType) {
        Delete.Builder builder = new Delete.Builder();
        builder.setHeader(PWDKEY, getSecret());
        builder.refresh(true);
        Delete delete = builder.index(indexName).type(indexType).build();
        try {
            JestResult result = jestClient.execute(delete);
            if (result != null && result.isSucceeded()) {
                throw new RuntimeException(result.getErrorMessage()+"删除类型失败!");
            }
        } catch (Exception e) {
            logger.error("删除类型失败", ExceptionUtils.getFullStackTrace(e));
            return false;
        }
        return true;
    }*/

    /**
     * 删除索引
     * @param indexId
     * @param indexName
     * @param indexType
     */
    /*public boolean deleteIndex( String indexName) {
        Delete.Builder builder = new Delete.Builder();
        //builder.setHeader(PWDKEY, getSecret());
        builder.refresh(true);
        Delete delete = builder.index(indexName).build();
        try {
            JestResult result = getInstance().execute(delete);
            if (result != null && result.isSucceeded()) {
                throw new RuntimeException(result.getErrorMessage()+"删除索引失败!");
            }
        } catch (Exception e) {
            //logger.error("删除索引失败", ExceptionUtils.getFullStackTrace(e));
            return false;
        }
        return true;
    }*/

}
